# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(lubridate)
library(readstata13)

# Import dataset
data <- read.dta13("data/survey_clean.dta")

# Make the plot
figureb5 <- data %>%
  select("sanct_phis_psoe_num", "sanct_verb_psoe_num", "sanct_insult_psoe_num", 
         "sanct_avoid_psoe_num", "sanct_deny_psoe_num", "sanct_gossip_psoe_num", "no_sanct_psoe_num",
         "exp_sanct_phis_psoe_num", "exp_sanct_verb_psoe_num", "exp_sanct_insult_psoe_num", 
         "exp_sanct_avoid_psoe_num", "exp_sanct_deny_psoe_num", "exp_sanct_gossip_psoe_num", "exp_no_sanct_psoe_num") %>%
  `colnames<-`(c("PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                 "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing",
                 "PSOE_Physical_Others", "PSOE_Verbal_Others", "PSOE_Insult_Others",
                 "PSOE_Avoid_Others", "PSOE_Deny_Others", "PSOE_Gossip_Others", "PSOE_Nothing_Others")) %>%
  # make into long format
  pivot_longer(cols = c(PSOE_Physical:PSOE_Nothing_Others),
               names_to = "Group") %>%
  group_by(Group) %>%
  mutate(n = sum(!is.na(value))) %>%
  mutate(mean = mean(value, na.rm = T)) %>%
  mutate(sd = sd(value, na.rm = T)) %>%
  mutate(std_error = sd/sqrt(n)) %>%
  mutate(ci_upper = mean + 1.96 * std_error) %>%
  mutate(ci_lower = mean - 1.96 * std_error) %>%
  ungroup() %>%
  mutate(Outcome = ifelse(Group %in% c("PSOE_Physical", "PSOE_Physical_Others"), "Physical",
                          ifelse (Group %in% c("PSOE_Verbal", "PSOE_Verbal_Others"), "Verbal",
                                  ifelse(Group %in% c("PSOE_Insult", "PSOE_Insult_Others"), "Insult",
                                         ifelse(Group %in% c("PSOE_Avoid", "PSOE_Avoid_Others"), "Avoid\ninteraction",
                                                ifelse(Group %in% c("PSOE_Deny", "PSOE_Deny_Others"), "Deny\nhelp",
                                                       ifelse(Group %in% c("PSOE_Gossip", "PSOE_Gossip_Others"), "Gossip",
                                                              "Do\nnothing"))))
                          ))) %>%
  mutate(Order = ifelse(Group %in% c("PSOE_Physical", "PSOE_Verbal", "PSOE_Insult",
                                     "PSOE_Avoid", "PSOE_Deny", "PSOE_Gossip", "PSOE_Nothing"), "Own views", "Perceptions of others' views")) %>%
  mutate(Nothing = ifelse(Group %in% c("PSOE_Nothing", "PSOE_Nothing_Others"), 1, 0)) %>%
  mutate(Outcome = fct_reorder(Outcome, Nothing)) %>%
  ggplot(aes(x = Outcome, y = value, color = Order)) +
  geom_point(alpha = 0.02, position = position_jitter()) +
  scale_color_manual(values = c("blue", "black")) +
  theme_bw() +
  geom_errorbar(aes(x = Outcome, ymin = ci_lower, ymax = ci_upper, width = 0.1)) + 
  geom_point(aes(x = Outcome, y = mean)) +
  theme(legend.position = "none") +
  facet_wrap(~Order) +
  labs(x = "", y = "") +
  scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6))
figureb5

ggsave("figures/figureb5.png", plot = figureb5, width = 20, height = 17.5, units = "cm")